package com.wcb.test.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 获取月初时间
	 * @param date
	 * @return
	 */
	public static Date getMonthStartDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);	
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		return calendar.getTime();
		
	}
	
	/**
	 * 获取月底时间
	 * @param date
	 * @return
	 */
	public static Date getMonthEndDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);	
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		int month = calendar.get(Calendar.MONTH);
		int year = calendar.get(Calendar.MONTH);
		
		switch (month) {
		case 0:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		case 1:
			if((year%4==0&&year%1!=0)||(year%400==0))
				calendar.set(Calendar.DAY_OF_MONTH,29);
			else
				calendar.set(Calendar.DAY_OF_MONTH,28);
			break;
		case 2:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		case 4:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		case 6:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		case 7:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		case 9:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		case 11:
			calendar.set(Calendar.DAY_OF_MONTH,31);
			break;
		default:
			calendar.set(Calendar.DAY_OF_MONTH,30);
			break;
		}
		return calendar.getTime();
		
	}
	
	
	public static void main(String ...args){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date retdate = getMonthEndDate(new Date());
		
		System.out.println(sdf.format(retdate));
	}
}
